package servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;

import dao.CityDao;
import dao.ProvinceDao;
import dao.impl.CityDaoImpl;
import dao.impl.CountyDaoImpl;
import dao.impl.ProvinceDaoImpl;
import entity.DetailAddress;
import entity.User;
import entity.User_Address;
import entity.User_Inf;
import service.UserService;
import service.impl.UserServiceImpl;
import wusiyuan.dao.ShowTheAddressDao;
import wusiyuan.dao.impl.ShowTheAddressDaoImpl;


public class GetConsigneeInfoServlet extends HttpServlet {
	private UserService us = new UserServiceImpl();
	private ShowTheAddressDao sad = new ShowTheAddressDaoImpl();
	private ProvinceDao pd = new ProvinceDaoImpl();
	private CityDao cd = new CityDaoImpl();
	private CountyDaoImpl cdi = new CountyDaoImpl();
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("application/json;charset=utf-8");
		//获取前端传回的uname
		String uname = request.getParameter("uname");
		//通过名字查询userInfo
		User_Inf user_Inf = us.queryUserByUsername(uname);
		//根据userid查询默认地址
		List<User_Address> list = sad.getUserAddressByUserId(user_Inf.getUserId());
		List<DetailAddress> dal = new ArrayList<DetailAddress>();
		//创建一个集合用于存储当前用户的所有详细地址信息
		if(list!=null){
			for (User_Address ua : list) {
				DetailAddress da = new DetailAddress();
				da.setDetailAddress(pd.getProvinceByProId(ua.getProvince()).getName()+
						cd.getCityByCityId(ua.getCity()).getName()+
						cdi.getCountyByCountyId(ua.getDistrict()).getName()+ua.getAddress());
				da.setZip_code(ua.getZip_code());
				dal.add(da);
			}
		}
		user_Inf.setList(dal);
		//将地址的所有信息添加到用户信息中
		response.getWriter().append(JSON.toJSONString(user_Inf));
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		doGet(request, response);
	}

}
